package demo15_Sync;

import com.sun.xml.internal.ws.util.CompletedFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author kunkun
 * @className Test01_Sync
 * @Description 测试异步执行，没有返回值
 * @date 2025/3/7 14:37
 */
public class Test01_Sync {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1. 启动异步任务
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            try {
                System.out.println("正在执行异步任务");
                TimeUnit.SECONDS.sleep(3);
                System.out.println("异步任务执行完毕");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        TimeUnit.SECONDS.sleep(1); // 确保异步任务有足够时间执行

        //2. 主线程执行
        System.out.println("主线程正在执行");

        //3. 等待异步任务执行完毕，阻塞执行
        completableFuture.get();

    }


}
